#!/bin/bash

echo "🔍 开始监控端口3000的错误..."
echo "时间: $(date)"
echo "=================================="

# 监控函数
monitor_port() {
    while true; do
        # 检查端口是否在监听
        if ! netstat -ano | grep -q ":3000.*LISTENING"; then
            echo "❌ $(date): 端口3000未在监听！"
        fi
        
        # 检查HTTP响应
        response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000 2>/dev/null)
        if [ "$response" != "200" ]; then
            echo "❌ $(date): HTTP响应异常 - 状态码: $response"
        fi
        
        # 检查DeepSeek API
        api_response=$(curl -s -o /dev/null -w "%{http_code}" -X POST "http://localhost:3000/api/deepseek/chat/completions" \
            -H "Content-Type: application/json" \
            -H "Authorization: Bearer sk-5005d6e1b94b449998aa8b896876e73e" \
            -d '{"model":"deepseek-chat","messages":[{"role":"user","content":"test"}],"stream":false}' \
            --connect-timeout 5 --max-time 10 2>/dev/null)
        
        if [ "$api_response" != "200" ]; then
            echo "❌ $(date): DeepSeek API异常 - 状态码: $api_response"
        else
            echo "✅ $(date): 所有服务正常"
        fi
        
        # 等待5秒
        sleep 5
    done
}

# 捕获中断信号
trap 'echo "监控已停止"; exit 0' INT TERM

# 开始监控
monitor_port

